package com.dream.testtubebaby.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentTabHost;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.widget.ImageView;
import android.widget.TabHost.TabSpec;
import android.widget.TextView;
import android.widget.Toast;
import cn.sharesdk.framework.ShareSDK;

import com.dream.testtubebaby.R;
import com.dream.testtubebaby.fragment.AccountFragment;
import com.dream.testtubebaby.fragment.AddFunctionFragment;
import com.dream.testtubebaby.fragment.BbsFragment;
import com.dream.testtubebaby.fragment.HomeFragment;
import com.dream.testtubebaby.fragment.ProductFragment;
import com.dream.testtubebaby.utils.SharePreferencesManager;
import com.dream.testtubebaby.utils.StatusBarCompat;

public class MainActivity extends AppCompatActivity implements
		AddFunctionFragment.BackHandledInterface {
	private FragmentTabHost mTabHost;
	private LayoutInflater layoutInflater;

	private Class<?> fragmentArray[] = { HomeFragment.class, BbsFragment.class,
			ProductFragment.class, AccountFragment.class };

	private int mImageViewArray[] = { R.drawable.tab_home_btn,
			R.drawable.tab_bbs_btn, R.drawable.tab_else_btn,
			R.drawable.tab_peple_btn };
	private String mTextviewArray[];

	private long firstBackTime;

	private AddFunctionFragment backFragment = null;

	private SharePreferencesManager mPreferencesManager;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mPreferencesManager = new SharePreferencesManager(this);
		mPreferencesManager.switchLanguage(mPreferencesManager.readLanguageCode());
		
		setContentView(R.layout.activity_main);
		StatusBarCompat.compat(this,
				getResources().getColor(R.color.status_bar_color));
		initView();

		ShareSDK.initSDK(this);
		// ShareSDK.registerPlatform(LaiwangCustomize.class);
		ShareSDK.setConnTimeout(20000);
		ShareSDK.setReadTimeout(20000);
	}

	@Override
	protected void onResume() {
		super.onResume();
	}

	private void initView() {
		layoutInflater = LayoutInflater.from(this);
		mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
		mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
		mTextviewArray = getResources().getStringArray(R.array.tab_title);

		int count = fragmentArray.length;

		for (int i = 0; i < count; i++) {
			TabSpec tabSpec = mTabHost.newTabSpec(mTextviewArray[i])
					.setIndicator(getTabItemView(i));
			mTabHost.addTab(tabSpec, fragmentArray[i], null);
		}
		mTabHost.setCurrentTab(0);
	}

	private View getTabItemView(int index) {
		View view = layoutInflater.inflate(R.layout.tab_item_view, null);

		ImageView imageView = (ImageView) view.findViewById(R.id.imageview);
		imageView.setImageResource(mImageViewArray[index]);

		TextView textView = (TextView) view.findViewById(R.id.textview);
		textView.setText(mTextviewArray[index]);

		return view;
	}

	@Override
	public void onBackPressed() {
		if (backFragment != null && backFragment.getCanGobackFlag()) {
			backFragment.webViewGoback();
			return;
		}

		if (firstBackTime + 2000 > System.currentTimeMillis()) {
			super.onBackPressed();
		} else {
			Toast.makeText(getApplicationContext(),
					getResources().getString(R.string.back_finish_msg),
					Toast.LENGTH_SHORT).show();
		}
		firstBackTime = System.currentTimeMillis();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		// getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public void onBackPressFragment(AddFunctionFragment fragment) {
		backFragment = fragment;
	}

}
